﻿using Shop.Model.AdvertPlace;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.AdvertPlace.Model;
using Shop.Repository.LinqHelper;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class AdvertPlaceRepository : ShopBaseRepository<DBAdvertPlace, long>, IAdvertPlaceRepository
    {
        public AdvertPlaceRepository ( ISqlClientFactory factory ) : base(factory)
        {
        }
        public long Add ( PlaceAdd add )
        {
            DBAdvertPlace place = add.ConvertMap<PlaceAdd, DBAdvertPlace>();
            place.Id = IdentityHelper.CreateId();
            place.PlaceStatus = AdvertPlaceStatus.起草;
            base.Insert(place);
            return place.Id;
        }
        public Result[] Gets<Result> ( AdvertPlaceGetParam query ) where Result : class, new()
        {
            return base.Gets<Result>(query.ToWhere());
        }
        public Result[] Query<Result> ( AdvertPlaceQueryParam query, IBasicPage paging, out int count ) where Result : class
        {
            paging.InitOrderBy("Id", true);
            return base.Query<Result>(query.ToWhere(), paging, out count);
        }
        public void SetStatus ( DBAdvertPlace place, AdvertPlaceStatus status )
        {
            if ( !base.Update(a => a.PlaceStatus == status, a => a.Id == place.Id) )
            {
                throw new ErrorException("shop.advert.place.update.fail");
            }
        }
    }
}
